<?php
$serv = new swoole_websocket_server("0.0.0.0",3999);
//服务的基本设置
$serv->set(array(
    'worker_num' => 2,
    'reactor_num'=>8,
    'task_worker_num'=>1,
    'dispatch_mode' => 2,
    'debug_mode'=> 1,
    'daemonize' => true,
    'log_file' => __DIR__.'/log/webs_swoole.log',
    'heartbeat_check_interval' => 60,
    'heartbeat_idle_time' => 600,
));

$serv->on('connect', function ($serv,$fd){
    echo "client:$fd Connect.".PHP_EOL;
});

$serv->on("receive",function(swoole_server $serv,$fd,$from_id,$data){
    echo "receive#{$from_id}: receive $data ".PHP_EOL;
});

$serv->on('open', function($server, $req) {
    echo "server#{$server->worker_pid}: handshake success with fd#{$req->fd}".PHP_EOL;;
});

$serv->on('message',function($server,$frame) {
    $msg['data']=$frame->data;
    switch ($msg['type']){
        case 'login':
            $server->push($frame->fd,"欢迎欢迎~");
        break;
        default:
        break;
    }
    $msg['fd']=$frame->fd;
    $server->task($msg);
});

$serv->on("workerstart",function($server,$workerid){
    echo "workerstart: ".$workerid.PHP_EOL;
});

$serv->on("task","on_task");

$serv->on("finish",function($serv,$task_id,$data){
    echo "finish";
    return;
});
$serv->on('close', function($server,$fd,$from_id) {
    $user_arr = array();
    $user_arr['stat'] = 'OK';
    $user_arr['type'] = 'Ulogout';
    $key = "client$fd";
    $chatid = $_SESSION["$key"];
    // file_put_contents('/var/www/html/swoole/swoole-src-1.8.7-stable/log.txt', "$key=>$chatid!$from_id$", FILE_APPEND);
    $nick = $_SESSION["nick_$key"];
    $user_arr['Ulogout'] = array('roomid'=>"1",'ChatId'=>"$chatid",'nick'=>"$nick",'sex'=>"0",'age'=>"0",'qx'=>"0",'ip'=>'','vip'=>'','color'=>'0','cam'=>'0','state'=>'0','mood'=>'');
    foreach ($server->connections as $conn) {
        $server->push($conn, json_encode($user_arr));
    }
    unset($_SESSION["$key"]);
    unset($_SESSION["nick_$key"]);
});

$serv->start();
function on_task($serv,$task_id,$from_id,$msg) {
    $data = $msg['data'];
    $data_arr = explode('|', $data);
    $str_arr = explode('=', $data_arr[0]);
    $type = $str_arr[0];
    if(!empty($type)){
        $chat_id = $data_arr[1];
        $room_id = $str_arr[2];
        $nick = $data_arr[2];
        $sex = $data_arr[3];
        $age = $data_arr[4];
        $qx = $data_arr[5];
        $ip = $data_arr[6];
        $vip = $data_arr[7];
        $color = $data_arr[8];
        $cam = $data_arr[9];
        $state = $data_arr[10];
        $mood = '';

        $fd = $msg['fd'];
        $key = "client".$fd;
        if($chat_id != false && $chat_id != 'false'){
            $_SESSION["$key"] = $chat_id;
    // file_put_contents('/var/www/html/swoole/swoole-src-1.8.7-stable/log.txt', "$key=>$chat_id@".$_SESSION["$key"].'#', FILE_APPEND);
            $_SESSION["nick_$key"] = $nick;
        }
        $chat_id = $_SESSION["$key"];
        $umsg_arr = array();
        $user_arr = array();
        if($type == "Login"){
            $show_type = "Ulogin";
            $user_arr = array();
            $user_arr['stat'] = 'OK';
            $user_arr['type'] = 'UonlineUser';
            $user_list = array();
            $user_list2 = array();
            foreach ($serv->connections as $conn) {
                $user_list['client_id'] = $conn + 10000;
                $chatid2 = $_SESSION["client$conn"];
                $nick2 = $_SESSION["nick_client$conn"];
                if($chatid2 != null){
                    $user_list['client_name'] = array('roomid'=>"1",'chatid'=>"$chatid2",'nick'=>"$nick2",'sex'=>"$sex",'age'=>"$age",'qx'=>"$qx",'ip'=>"$ip",'vip'=>"$vip",'color'=>"$color",'cam'=>"$cam",'state'=>"$state",'mood'=>"$mood");
                }
                $user_list2[] = $user_list;
            }
            $user_arr['roomListUser'] = $user_list2;
        }elseif($type == "SendMsg" || $type == "UMsg"){
            $show_type = "UMsg";
        }elseif($type == "Ulogout"){
            $show_type = "Ulogout";
        }
        $msg_arr['stat'] = 'OK';
        $msg_arr['type'] = $show_type;
        if ($type == 'SendMsg') {
            $umsg_arr['ChatId'] = "$chat_id";
            $umsg_arr['ToChatId'] = "{$str_arr[2]}";
            $umsg_arr["IsPersonal"] = "{$data_arr[1]}";
            $umsg_arr['Style'] = "{$data_arr[2]}";
            $umsg_arr['Txt'] = "{$data_arr[3]}";
        }else{
            $umsg_arr['roomid'] = 1;
            $umsg_arr['chatid'] = $chat_id;
            $umsg_arr['nick'] = $nick;
            $umsg_arr['sex'] = $sex;
            $umsg_arr['age'] = $age;
            $umsg_arr['qx'] = $qx;
            $umsg_arr['ip'] = $ip;
            $umsg_arr['vip'] = $vip;
            $umsg_arr['color'] = $color;
            $umsg_arr['cam'] = $cam;
            $umsg_arr['state'] = $state;
            $umsg_arr['mood'] = $mood;
        }
        $msg_arr["$show_type"] = $umsg_arr;
    }else{
        $msg_arr['type'] = 'ping';
    }
    foreach ($serv->connections as $conn) {
        $serv->push($conn,json_encode($msg_arr));
        if($type == 'Login' && $conn == $msg['fd']){
            $serv->push($conn,json_encode($user_arr));
        }
    }
    return;
}
function on_finish($serv,$task_id,$data){
    return true;
}
